package edu.hpu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * @Author 三分恶
 * @Date 2020/1/11
 * @Description
 */
@RestController
public class FileController {

    private static String UPLOADED_FOLDER = "G://temp//";

    @GetMapping("/")
    public String index() {
        return "uploadSingle";
    }

    @GetMapping("/more")
    public String uploadMore() {
        return "uploadMore";
    }


    /**
     * 上传单个文件
     *
     * @param file
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/upload")
    public String singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        //文件非空判断
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "请选择一个文件");
            return "文件为空，请重新上传";
        }
        try {
            // Get the file and save it somewhere
            byte[] bytes = file.getBytes();
            // UPLOADED_FOLDER ⽂件本地存储地址
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            //保存文件
            Files.write(path, bytes);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传文件成功！";
    }

    /**
     * 上传多个文件
     *
     * @param files
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/uploadMore")
    public String moreFileUpload(@RequestParam("file") MultipartFile[] files,
                                 RedirectAttributes redirectAttributes) {
        if (files.length == 0) {
            return "文件不能为空，请选择一个文件！";
        }
        for (MultipartFile file : files) {
            try {
                byte[] bytes = file.getBytes();
                Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
                Files.write(path, bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "所有文件上传成功！";
    }

    /**
     * 下载文件
     * @param response
     * @param fileName
     * @return
     */
    @GetMapping("/downloadFile")
    public String downloadFile(HttpServletResponse response, @RequestParam("fileName") String fileName) {
        File file = new File(UPLOADED_FOLDER+fileName);
        if (!file.exists()) {
            return "文件不存在！";
        }

        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);

        try {
            InputStream inStream = new FileInputStream(fileName);
            OutputStream os = response.getOutputStream();

            byte[] buff = new byte[1024];
            int len = -1;
            while ((len = inStream.read(buff)) > 0) {
                os.write(buff, 0, len);
            }
            os.flush();
            os.close();

            inStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "下载成功！";
    }
}
